﻿using System;
using System.Linq;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for RentEditorView.xaml
    /// </summary>
    public partial class RentEditorView
    {
        private readonly RentController _rentControl;
        private readonly Rent _rent;

        public RentEditorView()
        {
            InitializeComponent();

            _rentControl = new RentController();
            Title = "Add Rent";
        }

        public RentEditorView(Rent rent)
        {
            InitializeComponent();
            _rentControl = new RentController();
            Title = "Edit Rent";

            _rent = rent;

            textBoxId.Text = rent.Id.ToString();
            textBoxRate.Text = rent.Rate.ToString();
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxRate.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxRate.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                try
                {
                    var rateDigit = textBoxRate.Text.ToCharArray();

                    if (rateDigit.Any(cell => !char.IsDigit(cell)))
                    {
                        throw new Exception();
                    }

                    if (Title.Equals("Add Rent"))
                    {
                        var rent = new Rent
                                       {
                                           Rate = Int32.Parse(textBoxRate.Text)
                                       };

                        var message = _rentControl.AddRent(rent);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses menambah rent");
                                ClearTextBox();
                                break;
                            default:
                                MessageBox.Show("rate sudah ada");
                                break;
                        }
                    }
                    else
                    {
                        var newRent = new Rent
                        {
                            Id = _rent.Id,
                            Rate = Int32.Parse(textBoxRate.Text)
                        };

                        var message = _rentControl.EditRent(newRent, _rent.Rate);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses mengedit rent");
                                break;
                            default:
                                MessageBox.Show("rate sudah ada");
                                break;
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("field rate harus angka");
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
